package com.kung.qvs.ui.main.fragment;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.kung.qvs.data.JtRepository;
import com.kung.qvs.entity.PropInfoBean;
import com.kung.qvs.entity.database.SqlManager;
import com.kung.qvs.utils.Constants;

import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class CompanyInfoViewModel extends BaseViewModel<JtRepository> {

    private PropInfoBean propInfoBean1; //下载的公司信息
    private PropInfoBean propInfoBean2; //本地保存的公司信息

    public CompanyInfoViewModel(@NonNull Application application, JtRepository model) {
        super(application, model);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        loadLocalCompanyInfo();
    }

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();

    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent eventToLoginPage = new SingleLiveEvent<>();
        public SingleLiveEvent<String> eventChangeTrashType = new SingleLiveEvent<>();
        public SingleLiveEvent eventToSetBaseUrl = new SingleLiveEvent();   //设置请求ip地址
    }

    public ObservableField<String> mOriginPlace = new ObservableField<>("");    //产地
    public ObservableField<String> mProducer = new ObservableField<>("");    //生产者
    public ObservableField<String> mContact = new ObservableField<>("");    //联系方式
    public ObservableField<String> mElectronicScaleNo = new ObservableField<>("");    //打印前缀

    public BindingCommand onCommanBack = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToLoginPage.call();
        }
    });

    public BindingCommand onCommandSave = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            if (StringUtils.isTrimEmpty(mOriginPlace.get())) {
                ToastUtils.showShort("请输入产地");
                return;
            }
            if (StringUtils.isTrimEmpty(mProducer.get())) {
                ToastUtils.showShort("请输入生产者");
                return;
            }
            if (StringUtils.isTrimEmpty(mContact.get())) {
                ToastUtils.showShort("请输入联系方式");
                return;
            }
            PropInfoBean bean = new PropInfoBean();
            bean.set_id(null);
            bean.setLocalId(Constants.LOCAL_CODE_USER);
            if (propInfoBean1 != null) {
                bean.setBasis(propInfoBean1.getBasis());
                bean.setElectronicScaleNo(propInfoBean1.getElectronicScaleNo());
                bean.setPromise(propInfoBean1.getPromise());
            }
            bean.setOriginPlace(mOriginPlace.get());
            bean.setProducer(mProducer.get());
            bean.setContact(mContact.get());
            if(SqlManager.insertOrReplacePropInfoBean(bean) > 0){
                ToastUtils.showShort("保存成功");
            }
            loadLocalCompanyInfo();
        }
    });

    public BindingCommand onCommandReset = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            SqlManager.deletePropInfoBeanById(Constants.LOCAL_CODE_USER);
            loadLocalCompanyInfo();
        }
    });

    private void loadLocalCompanyInfo() {
        propInfoBean1 = SqlManager.queryPropInfoBeanById(Constants.LOCAL_CODE_DEFAULT);
        propInfoBean2 = SqlManager.queryPropInfoBeanById(Constants.LOCAL_CODE_USER);
        if (propInfoBean2 != null) {
            mOriginPlace.set(propInfoBean2.getOriginPlace());
            mProducer.set(propInfoBean2.getProducer());
            mContact.set(propInfoBean2.getContact());
            mElectronicScaleNo.set(propInfoBean2.getElectronicScaleNo());
        } else if (propInfoBean1 != null) {
            mOriginPlace.set(propInfoBean1.getOriginPlace());
            mProducer.set(propInfoBean1.getProducer());
            mContact.set(propInfoBean1.getContact());
            mElectronicScaleNo.set(propInfoBean1.getElectronicScaleNo());
        }
    }
}
